#define DEBUG
#include <cstdio>
#include <cstring>
#include <cmath>

using namespace std;

const int maxn=500, maxm=500;

int min(int a, int b) {
  return a<b ? a : b;
}

int solve(int sx, int sy, int tx, int ty, int n, int m, int wall[][maxn+1][maxm+1][2], char map[][maxm+1]) {
  static bool book[maxn+1][maxm+1];
  static int f[maxn+1][maxm+1], que[maxn*maxm+1][2];
  const int fx[4][2] = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
  memset(f, 126, sizeof(f));
  que[1][0] = sx, que[1][1] = sy;
  f[sx][sy] = 0;
  for (int h=0, t=1; h!=t; ) {
    h = h%(n*m)+1;
    int x=que[h][0], y=que[h][1];
    book[x][y] = false;
    for (int i=0; i<4; i++) {
      int nx = wall[i][x][y][0], ny = wall[i][x][y][1];
      if (!book[nx][ny]) {
	int temp=0x7ffffff;
	for (int j=0; j<4; j++) {
	  if (i!=j) {
	    temp=min(temp, f[x][y]+abs(x-wall[j][x][y][0])+abs(y-wall[j][x][y][1])+1);
	  }
	}
	if (f[nx][ny]>temp) {
	  f[nx][ny] = temp;
	  if (!book[nx][ny]) {
	    book[nx][ny] = true;
	    t = t%(n*m)+1;
	    que[t][0] = nx;
	    que[t][1] = ny;
	  }
	}
      }
    }
    for (int i=0; i<4; i++) {
      int nx=x+fx[i][0], ny=y+fx[i][1];
      if (nx>0 && ny>0 && nx<=n && ny<=m && map[nx][ny]!='#' && f[nx][ny]>f[x][y]+1) {
	f[nx][ny] = f[x][y]+1;
	if (!book[nx][ny]) {
	  book[nx][ny] = true;
	  t = t%(n*m)+1;
	  que[t][0] = nx;
	  que[t][1] = ny;
	}
      }
    }
  }
  return f[tx][ty];
}

int main() {
  freopen("cell.in", "r", stdin);
  freopen("cell.out", "w", stdout);

  static char map[maxn+1][maxm+1];
  int n, m, sx, sy, tx, ty;
  scanf("%d %d", &n, &m);
  getchar();
  for (int i=1; i<=n; i++) {
    for (int j=1; j<=m; j++) {
      map[i][j] = getchar();
      if (map[i][j]=='C') {
	sx = i, sy = j;
      } else if (map[i][j]=='F') {
	tx = i, ty = j;
      }
    }
    getchar();
  }

  static int wall[4][maxn+1][maxm+1][2];
  for (int i=1; i<=n; i++) {
    for (int j=1; j<=m; j++) {
      if (map[i-1][j]=='#') {
	wall[0][i][j][0] = i;
	wall[0][i][j][1] = j;
      } else {
	wall[0][i][j][0] = wall[0][i-1][j][0];
	wall[0][i][j][1] = wall[0][i-1][j][1];
      }
      if (map[i][j-1]=='#') {
	wall[1][i][j][0] = i;
	wall[1][i][j][1] = j;
      } else {
	wall[1][i][j][0] = wall[1][i][j-1][0];
	wall[1][i][j][1] = wall[1][i][j-1][1];
      }
    }
  }
  for (int i=n; i>0; i--) {
    for (int j=m; j>0; j--) {
      if (map[i+1][j]=='#') {
	wall[2][i][j][0] = i;
	wall[2][i][j][1] =  j;
      } else {
	wall[2][i][j][0] = wall[2][i+1][j][0];
	wall[2][i][j][1] = wall[2][i+1][j][1];
      }
      if (map[i][j+1]=='#') {
	wall[3][i][j][0] = i;
	wall[3][i][j][1] = j;
      } else {
	wall[3][i][j][0] = wall[3][i][j+1][0];
	wall[3][i][j][1] = wall[3][i][j+1][1];
      }
    }
  }

  int t=solve(sx, sy, tx, ty, n, m, wall, map);
  if (t<=n*m) {
    printf("%d", t);
  } else {
    printf("no");
  }

  fclose(stdin);
  fclose(stdout);
  return 0;
}
